//狼人杀
module.exports.createRoom = function(roomId,channelService,GameRemote) {
	console.log("狼人杀new!!55555创建房间:"+roomId)
	var gameBasic = require("./basic/gameBasic.js")
	var local = {}
	//身份定义
	var roleClass = {				//protoss   神民   villager   村民   werewolf 狼阵营
		"seer" : "protoss",        	//"预言家"
		"witch" : "protoss", 	 	//"女巫"
		"hunter" : "protoss",		//"猎人"
		"idiots" : "protoss",		//"白痴"
		"villager" : "villager", 	//"村民"
		"werewolf" : "werewolf"		//"狼人"
	}
	//本局身份
	// var roleList = ["seer","witch","hunter","idiots","villager","villager","villager","villager","werewolf","werewolf","werewolf","werewolf"]
	var roleList = ["werewolf","seer","witch","hunter","idiots","villager","villager","villager","villager","werewolf","werewolf","werewolf"]
	var roleMap = {} //角色对应椅子号列表
	var playerNum = 1
	var room = new gameBasic(roomId,"hnMahjong",playerNum,channelService,GameRemote)
	//添加解散模块
	var dissolution = require("./basic/dissolution.js")
	dissolution.call(room)
	room.nextTime = false
	room.nextFun = false 				//下一个方法
	room.storageFun = false 			//存储方法

	room.day = 1 						//天数

	var KONGXIAN_STATE = 0 				//空闲阶段
	var NONE_STATE = -1 				//不可操作阶段
	var PRE_STAET = 1 					//预开始阶段
	var NIGHT_WEREWOLF_STATE = 2        //夜晚狼人阶段
	var NIGHT_SEER_STATE = 3        	//夜晚预言家阶段
	var NIGHT_WITCH_STATE = 4        	//夜晚女巫阶段
	var NIGHT_HUNTER_STATE = 5        	//夜晚猎人阶段
	var DAY_RUNSHERIFF_JOIN = 6     	//竞选警徽上警阶段
	var DAY_RUNSHERIFF_SAY1 = 7			//竞选警徽发言阶段
	var DAY_RUNSHERIFF_VOTE = 8     	//竞选警徽投票阶段
	var DAY_RUNSHERIFF_SAY2 = 9			//竞选警徽发言阶段
	var LAST_WORDS = 10 				//首日遗言阶段
	var DAY_HUNTER_READY = 11 			//猎人等待开枪状态
	var DAY_PLAYER_SAY_READY = 12 		//准备开始放逐发言阶段
	var DAY_PLAYER_SAY_1 = 13   		//玩家发言阶段
	var DAY_BANISH_VOTE = 14 			//放逐投票阶段
	var DAY_PLAYER_SAY_2 = 15           //第二轮放逐发言
	var SHERIFF_CHANGE = 16 			//警徽移交阶段

	room.state = room.KONGXIAN_STATE 		//当前状态
	room.nextState = false 				//下一状态

	var nightWerewolfTime = 10000    	//狼人杀人时间
	var nightSeerTime = 1000 			//预言家验人时间
	var nightWitchTime = 1000  			//女巫操作时间
	var nightHunterTime = 1000 			//猎人查看时间
	var runSheriffJoinTime = 1000       //上警时间
	var runSheriffJoinVote = 1000       //警长投票时间
	var banishVoteTime = 1000 			//放逐投票时间


	room.day = 1 						//天数
	room.diurnal = "night" 		        //白天黑夜状态
	room.houseOwner = -1 				//房主uid
	room.sheriffChair = -1 				//警长椅子号
	room.oldSheriffChair = -1 			//旧警长椅子号
	room.killList = {} 					//狼人猎杀目标列表
	room.killTarget = false 			//狼人最终猎杀目标
	room.witchAgentia = {"save" : true,"poison" : true}  //女巫药剂
	room.witchAction = {"type" : false,"target" : false} //女巫操作
	room.joinRunSheriffList = {} 		//参与警长竞选的玩家列表
	room.voteList = {} 					//投票列表
	room.votedList = {} 				//被投票列表
	room.surviveState = {} 				//存活状态表
	room.dieReason = {} 				//死亡原因表
	room.hunterGun = false 				//猎人开枪状态
	room.showRole = {} 					//展示角色状态
	room.wantRole = {} 					//期望身份
	room.mainUpdate = function(curTime) {
		if(room.nextTime && curTime > room.nextTime){
			room.nextTime = false
			var fun = room.nextFun
			if(fun){
				room.nextFun = false
				fun()
			}
		}
	}
	room.setBeforeInfo = function(info) {
		if(info && info.wantRole && typeof(info.wantRole) == "object"){
			for(var i in info.wantRole){
				room.wantRole[i] = info.wantRole[i]
			}
		}
	}
	//开始游戏，初始化、发身份牌
	room.gameBegin = function() {
		room.state = room.PRE_STAET
		//随机分配身份
		roleList.sort(function(a,b) {return Math.random() < 0.5 ? 1 : -1})
		for(var i in room.wantRole){
			for(var j = 0;j < roleList.length;j++){
				if(roleList[j] == room.wantRole[i]){
					//交换位置
					var tmp = roleList[i]
					roleList[i] = roleList[j]
					roleList[j] = tmp
				}
			}
		}
		console.log(roleList)
		roleMap = {}
		room.surviveState = {}
		room.witchAgentia = {"save" : true,"poison" : true}
		room.day = 1
		room.sheriffChair = -1
		room.hunterGun = false
		//发放身份牌
		for(var i = 0;i < roleList.length;i++){
			if(!roleMap[roleList[i]]){
				roleMap[roleList[i]] = []
			}
			roleMap[roleList[i]].push(i)
			room.surviveState[i] = true
			room.showRole[i] = "normal"
			if(room.players[i] && room.players[i].isActive){
				var notify = {
					cmd : "gameBegin",
					role : roleList[i],
					chair : i
				}
				room.sendByUid(room.players[i].uid,notify)
			}
		}

		console.log(roleMap)
	}
	room.joinLaterFun = function(chair) {
		if(room.houseOwner == -1){
			console.log("room.houseOwner ： "+room.players[chair].uid)
			room.houseOwner = room.players[chair].uid
		}
  	}
	//向某个身份的玩家发消息
	local.sendByRole = function(role,notify) {
		if(roleMap[role]){
			for(var i = 0;i < roleMap[role].length;i++){
				var chair = roleMap[role][i]
				if(room.surviveState[chair] === true && room.players[chair] && room.players[chair].isActive){
					room.sendByUid(room.players[chair].uid,notify)
				}
			}
		}
	}
	//给房主发消息
	local.sendByHouseOwner = function(notify) {
		room.sendByUid(room.houseOwner,notify)
	}
	//游戏开始
	local.startGame = function() {
		room.state = room.NIGHT_WEREWOLF_STATE
		var notify = {
			cmd : "startGame"
		}
		room.sendAll(notify)
		local.nightWerewol()
	}
	//夜晚狼人阶段
	local.nightWerewol = function() {
		room.killList = {}
		room.killTarget = false
		room.diurnal = "night"
		room.sendAll({cmd : "werewolfState"})
		var notify = {
			cmd : "werewolfAction",
			werewols : roleMap["werewolf"],
			time : nightWerewolfTime
		}
		local.sendByRole("werewolf",notify)
		room.nextTime = (new Date()).getTime() + nightWerewolfTime + 1000
		room.nextFun = local.nightWerewolKillend
	}
	//狼人刀人结束
	local.nightWerewolKillend = function() {
		var target = false
		var maxNum = 0
		var targetList = {}
		for(var i in room.killList){
			if(!targetList[room.killList[i]]){
				targetList[room.killList[i]] = 0
			}
			targetList[room.killList[i]]++
		}
		for(var i in targetList){
			if(targetList[i] > maxNum){
				target = i
				maxNum = targetList[i]
			}
		}
		room.killTarget = target
		local.nightSeer()
	}
	//夜晚预言家阶段
	local.nightSeer = function() {
		room.state = room.NIGHT_SEER_STATE
		room.sendAll({cmd : "seerState"})
		room.nextFun = local.nightWitch
		if(room.surviveState[roleMap["seer"][0]] !== true){
			room.nextTime = (new Date()).getTime() + Math.floor(Math.random() * 5000) + 5000
		}else{
			var notify = {
				cmd : "seerAction",
				time : nightSeerTime
			}
			local.sendByRole("seer",notify)
			room.nextTime = (new Date()).getTime() + nightSeerTime + 1000
		}
	}
	//夜晚女巫阶段
	local.nightWitch = function() {
		room.state = room.NIGHT_WITCH_STATE
		room.witchAction = {"type" : false,"target" : false}
		room.sendAll({cmd : "witchState"})
		room.nextFun = local.nightHunter
		if(room.surviveState[roleMap["witch"][0]] != true){
			room.nextTime = (new Date()).getTime() + Math.floor(Math.random() * 5000) + 5000
		}else{
			var notify = {
				cmd : "witchAction",
				time : nightWitchTime
			}
			if(room.witchAgentia["save"]){
				notify.killTarget = room.killTarget
			}else{
				notify.killTarget = "您已经用过解药，不能知道今晚狼人到了谁"
			}
			local.sendByRole("witch",notify)
			room.nextTime = (new Date()).getTime() + nightWitchTime + 1000
		}
	}
	//夜晚猎人阶段
	local.nightHunter = function() {
		room.state = room.NIGHT_HUNTER_STATE
		room.sendAll({cmd : "hunterState"})
		room.nextFun = local.enterDaytime
		var chair = roleMap["hunter"][0]
		if(room.surviveState[chair] !== true){
			room.nextTime = (new Date()).getTime() + Math.floor(Math.random() * 5000) + 5000
		}else{
			var notify = {
				cmd : "hunterAction",
				time : nightSeerTime
			}
			if(room.witchAction["type"] == "poison" && room.witchAction["target"] == chair){
				notify.state = false
			}else{
				notify.state = true
			}
			local.sendByRole("hunter",notify)
			room.nextTime = (new Date()).getTime() + nightHunterTime
		}
	}
	//白天阶段
	local.enterDaytime = function() {
		room.day++
		room.diurnal = "day"
		room.sendAll({cmd : "enterDaytime"})
		if(room.day == 2){
			//第二天白天竞选警徽
			local.runSheriffJoin()
		}else{
			//其余直接宣布结果
			local.announceResult()
		}
	}
	//上警阶段
	local.runSheriffJoin = function() {
		room.state = room.DAY_RUNSHERIFF_JOIN
		room.sendAll({cmd : "runSheriffJoin",time : runSheriffJoinTime})
		room.joinRunSheriffList = {}
		room.nextFun = local.runSheriffSay1
		room.nextTime = (new Date()).getTime() + runSheriffJoinTime
	}
	//报名参与竞选
	room.handle.runSheriffJoin = function(uid,sid,param,cb) {
		if(room.state != DAY_RUNSHERIFF_JOIN){
			cb(false,"不在竞选警长阶段")
			return
		}
		var chair = room.chairMap[uid]
		if(!room.joinRunSheriffList[chair]){
			room.joinRunSheriffList[chair] = true
			cb(true)
		}else{
			cb(false,"已报名")
		}
	}
	//竞选警长发言阶段
	local.runSheriffSay1 = function() {
		room.state = room.DAY_RUNSHERIFF_SAY1
		//随机一个玩家开始发言
		var tmpList = []
		for(var i in room.joinRunSheriffList){
			tmpList.push(i)
		}
		if(tmpList.length == 1){
			local.confirmSheriff(tmpList[0])
		}else if(tmpList.length){
			var rand = tmpList[Math.floor(Math.random() * tmpList.length)]
			room.nextFun = local.runSheriffEnd1
			room.sendAll({cmd : "runSheriffSay1",rand : rand,joinRunSheriffList : room.joinRunSheriffList})
		}else{
			local.sheriffDrain()
		}
	}
	//开始警上投票
	room.handle.beginSheriffVote = function(uid,sid,param,cb) {
		if(room.houseOwner == uid && (room.state == DAY_RUNSHERIFF_SAY1 || room.state == DAY_RUNSHERIFF_SAY2)){
			local.runSheriffVote()
			cb(true)
		}else{
			cb(false)
		}
	}
	//警上第一轮投票阶段
	local.runSheriffVote = function() {
		room.state = room.DAY_RUNSHERIFF_VOTE
		room.voteList = {}
		room.sendAll({cmd : "runSheriffVote",time : runSheriffJoinVote,joinRunSheriffList : room.joinRunSheriffList})
		room.nextTime = (new Date()).getTime() + runSheriffJoinVote
	}
	//警上投票
	room.handle.sheriffVote = function(uid,sid,param,cb) {
		//未竞选可以投票
		var chair = room.chairMap[uid]
		if(room.joinRunSheriffList[chair] == undefined){
			var target = param.target
			if(typeof(target) == "number" && room.joinRunSheriffList[target] == true && room.voteList[chair] == undefined){
				room.voteList[chair] = target
				cb(true)
			}else{
				cb(false)
			}
		}else{
			cb(false)
		}
	}
	//警上第一轮投票结束
	local.runSheriffEnd1 = function() {
		var maxTarget = -1
		var maxVoteCount = 0
		var tmpVoteList = {}
		//记录投票人数
		for(var i in room.voteList){
			if(!tmpVoteList[room.voteList[i]]){
				tmpVoteList[room.voteList[i]] = 0
			}
			tmpVoteList[room.voteList[i]]++
		}
		for(var i in tmpVoteList){
			if(tmpVoteList[i] > maxVoteCount){
				maxTarget = i
				maxVoteCount = tmpVoteList[i]
			}
		}
		if(maxTarget == -1){
			//无人投票、警徽流失
			local.sheriffDrain()
		}else{
			var tmpCount = 0
			for(var i in tmpVoteList){
				if(tmpVoteList[i] >= maxVoteCount){
					tmpCount++ 
				}
			}
			if(tmpCount > 1){
				//多人平票，第二轮发言
				for(var i in room.joinRunSheriffList){
					if(tmpVoteList[i] < maxVoteCount){
						delete room.joinRunSheriffList[i]
					}
				}
				local.runSheriffSay2()
			}else{
				//选出警长
				local.confirmSheriff(maxTarget)
			}
		}
	}
	//竞选警长第二轮发言阶段
	local.runSheriffSay2 = function() {
		room.state = room.DAY_RUNSHERIFF_SAY2
		//随机一个玩家开始发言
		var tmpList = []
		for(var i in room.joinRunSheriffList){
			tmpList.push(room.joinRunSheriffList[i])
		}
		var rand = tmpList[Math.floor(Math.random() * tmpList.length)]
		room.nextFun = local.runSheriffEnd2
		room.sendAll({cmd : "runSheriffSay2",rand : rand,joinRunSheriffList : room.joinRunSheriffList})
	}
	//竞选警长第二轮投票阶段
	local.runSheriffVote2 = function() {
		room.state = room.DAY_RUNSHERIFF_VOTE
		room.voteList = {}
		room.sendAll({cmd : "runSheriffVote",time : runSheriffJoinVote,joinRunSheriffList : room.joinRunSheriffList})
		room.nextTime = (new Date()).getTime() + runSheriffJoinVote
	}
	//警上第二轮投票结束
	local.runSheriffEnd2 = function() {
		var maxTarget = -1
		var maxVoteCount = 0
		var tmpVoteList = {}
		//记录投票人数
		for(var i in room.voteList){
			if(!tmpVoteList[room.voteList[i]]){
				tmpVoteList[room.voteList[i]] = 0
			}
			tmpVoteList[room.voteList[i]]++
		}
		for(var i in tmpVoteList){
			if(tmpVoteList[i] > maxVoteCount){
				maxTarget = i
				maxVoteCount = tmpVoteList[i]
			}
		}
		if(maxTarget == -1){
			//无人投票、警徽流失
			local.sheriffDrain()
		}else{
			var tmpCount = 0
			for(var i in tmpVoteList){
				if(tmpVoteList[i] >= maxVoteCount){
					tmpCount++ 
				}
			}
			if(tmpCount > 1){
				//多人平票，警徽流失
				local.sheriffDrain()
			}else{
				//选出警长
				local.confirmSheriff(maxTarget)
			}
		}
	}
	//警徽流失
	local.sheriffDrain = function() {
		room.state = room.NONE_STATE
		room.sheriffChair = -1
		room.sendAll({cmd : "sheriffDrain"})
		room.nextFun = local.announceResult
		room.nextTime = (new Date()).getTime() + 5000
	}
	//选出警长
	local.confirmSheriff = function(chair) {
		room.state = room.NONE_STATE
		room.sheriffChair = chair
		room.sendAll({cmd : "confirmSheriff", sheriffChair : room.sheriffChair})
		room.nextFun = local.announceResult
		room.nextTime = (new Date()).getTime() + 5000
	}
	//退水
	room.handle.quitElection = function(uid,sid,param,cb) {
		if(room.state != DAY_RUNSHERIFF_SAY1 && room.state != DAY_RUNSHERIFF_SAY2){
			cb(false,"不在警上发言阶段")
			return
		}
		var chair = room.chairMap[uid]
		if(room.joinRunSheriffList[chair] !== true){
			cb(false,"未竞选")
			return
		}
		room.joinRunSheriffList[chair] = false
		room.sendAll({cmd : "quitElection", chair : chair})
		cb(true)
	}
	//宣布夜晚结果
	local.announceResult = function() {
		var flag = false
		var dielist = {}
		if(room.killTarget !== false && room.witchAction.type !== "save"){
			dielist[room.killTarget] = "werewolf"
			flag = true
		}
		if(room.witchAction.type == "poison"){
			dielist[room.witchAction.target] = "poison"
			flag = true
		}
		//下一阶段为放逐发言阶段
		room.nextFun = local.banishSpeak1
		room.state = room.DAY_PLAYER_SAY_READY
		if(flag){
			if(room.day == 2){
				//首夜有人死亡进入遗言阶段
				room.state = room.LAST_WORDS
				for(var i in dielist){
					local.dieDispose(i,dielist[i],true)
				}
			}else{
				//非首夜  进入发言阶段
				for(var i in dielist){
					local.dieDispose(i,dielist[i],false)
				}
			}
		}
		for(var i in dielist){
			dielist[i] = true
		}
		room.sendAll({cmd : "announceResult", dielist : dielist})
	}
	//仅公布结果
	local.openResult = function() {
		var flag = false
		var dielist = {}
		if(room.killTarget !== false && room.witchAction.type !== "save"){
			dielist[room.killTarget] = "werewolf"
			flag = true
		}
		if(room.witchAction.type == "poison"){
			dielist[room.witchAction.target] = "poison"
			flag = true
		}
		if(flag){
			for(var i in dielist){
				local.dieDispose(i,dielist[i],false)
			}
		}
		for(var i in dielist){
			dielist[i] = true
		}
		room.sendAll({cmd : "announceResult", dielist : dielist})
	}
	//警徽移交阶段
	local.sheriffChange = function() {
		room.state = room.SHERIFF_CHANGE
		room.nextFun = local.sheriffChangeEnd
		room.oldSheriffChair = room.sheriffChair
		room.sheriffChair = -1
		room.nextTime = (new Date()).getTime() + 15000
		var notify = {
			cmd : "curState",
			state : "sheriffChange",
			time : 15000
		}
		room.sendAll(notify)
	}
	//警徽移交结束
	local.sheriffChangeEnd = function() {
		room.oldSheriffChair = -1
		room.nextFun = room.storageFun
		room.storageFun = false
		var notify = {
			cmd : "sheriffChangeEnd",
			sheriffChair : room.sheriffChair
		}
		room.sendAll(notify)
		room.nextTime = (new Date()).getTime() + 5000
	}
	//玩家移交警徽
	room.handle.sheriffChange = function(uid,sid,param,cb) {
		if(room.state != SHERIFF_CHANGE){
			cb(false,"不在警徽移交阶段")
			return
		}
		var chair = room.chairMap[uid]
		if(room.oldSheriffChair != chair){
			cb(false,"非警长")
			return
		}
		var target = param.target
		if(typeof(target) != "number" || target < 0 || target >= roleList.length || room.surviveState[target] != true){
			cb(false,"sheriffChair error : "+sheriffChair)
			return
		}
		room.sheriffChair = sheriffChair
		cb(true)
		room.nextTime = (new Date()).getTime()
	}
	//角色退场处理阶段
	local.dieDispose = function(chair,reason,lastWord) {
		if(lastWord){
			state = LAST_WORDS
		}
		room.surviveState[chair] = false
		if(reason == "poison" || reason == "werewolf"){
			room.showRole[chair] = "die-none"
		}
		switch(reason){
			case "poison":
				//被毒
			case "werewolf":
				//被刀
				//均不显示死亡信息
				room.showRole[chair] = "die-none"
			break
			case "banish":
				//被放逐
				if(roleList[chair] == "idiots"){
					//白痴翻牌不死
					room.surviveState[chair] = true
					room.showRole[chair] = "survive-idiots"
				}else{
					room.showRole[chair] = "die-banish"
				}
			break
			case "gun":
				//被开枪
				room.showRole[chair] = "die-gun"
			break
			case "hunter":
				//猎人翻牌
				room.showRole[chair] = "die-hunter"
			break
			case "boom":
				//狼人自爆
				room.showRole[chair] = "die-boom"
			break
		}
		if(roleList[chair] == "hunter"){
			var state = true
			if(reason == "poison"){
				state = false
			}
			if(state){
				//可以开枪则进入开枪状态
				room.nextState = room.state
				room.state = room.DAY_HUNTER_READY
				room.hunterGun = true
			}
			//猎人死亡告知是否可以开枪
			room.sendByUid(room.players[chair].uid,{cmd : "shootingState",state : state})
		}
		//警长死亡移交警徽
		if(room.surviveState[chair] != true && room.sheriffChair == chair){
			room.storageFun = room.nextFun
			room.nextFun = local.sheriffChange
		}
		var notify = {
			cmd : "dieDispose",
			chair : chair,
			showRole : room.showRole[chair],
			lastWord : lastWord,
			state : room.state
		}
		room.sendAll(notify)
	}
	//猎人行动结束
	local.hunterNone = function() {
		if(room.nextState){
			room.state = room.room.nextState
		}
		room.nextState = false
		room.hunterGun = false
		//法官在state 为 DAY_PLAYER_SAY_1 时显示开始发言    为 LAST_WORDS 显示结束遗言
		var notify = {
			cmd : "curState",
			state : room.state
		}
		room.sendAll(notify)
	}
	//猎人选择是否开枪
	room.handle.hunterAction = function(uid,sid,param,cb) {
		if(room.state != DAY_HUNTER_READY){
			cb(false,"不在猎人开枪阶段")
			return
		}
		var chair = room.chairMap[uid]
		if(roleList[chair] !== "hunter"){
			cb(false,"身份错误：hunter : "+roleList[room.chairMap[uid]])
			return
		}
		var target = param.target
		if(typeof(target) != "number" || target < -1 || target >= roleList.length || room.surviveState[target] !== true){
			cb(false,"目标错误 : "+target)
			return
		}
		if(!room.hunterGun){
			cb(false,"不在开枪状态")
			return
		}
		if(target == -1){
			local.hunterNone()
		}else{
			local.hunterNone()
			local.dieDispose(target,"gun",false)
			var notify = {
				cmd : "hunterGun",
				chair : chair,
				target : target,
				showRole : room.showRole[chair],
			}
			room.sendAll(notify)
		}
		cb(true)
	}
	//房主选择结束遗言
	room.handle.finishLastWords = function(uid,sid,param,cb) {
		if(room.state != LAST_WORDS){
			cb(false,"不在遗言阶段")
			return
		}
		if(uid != room.houseOwner){
			cb(false,"房主才能操作")
			return
		}
		cb(true)
		room.nextTime = (new Date()).getTime()
	}
	//房主选择开始放逐发言
	room.handle.beginBanishSpeak = function(uid,sid,param,cb) {
		if(room.state != DAY_PLAYER_SAY_READY){
			cb(false,"不在开始放逐发言阶段")
			return
		}
		if(uid != room.houseOwner){
			cb(false,"房主才能操作")
			return
		}
		cb(true)
		room.nextTime = (new Date()).getTime()
	}
	//第一轮放逐发言阶段
	local.banishSpeak1 = function() {
		console.log("banishSpeak1")
		room.state = room.DAY_PLAYER_SAY_1
		room.nextFun = local.banishend1
		var notify = {
			cmd : "banishSpeak1"
		}
		room.sendAll(notify)
	}
	//房主选择开始放逐投票
	room.handle.beginBanishVote = function(uid,sid,param,cb) {
		if(room.houseOwner == uid && (room.state == DAY_PLAYER_SAY_1 || room.state == DAY_PLAYER_SAY_2)){
			local.banishVote()
			cb(true)
		}else{
			cb(false)
		}
	}
	//放逐投票
	room.handle.banishVote = function(uid,sid,param,cb) {
		//存活且有投票权可以投票
		var chair = room.chairMap[uid]
		if(surviveState[chair] && room.showRole[chair] != "survive-idiots"){
			var target = param.target
			if(typeof(target) == "number" && room.voteList[chair] == undefined && room.showRole[target] != "survive-idiots"){
				room.voteList[chair] = target
				cb(true)
			}else{
				cb(false)
			}
		}
	}
	//放逐投票阶段
	local.banishVote = function() {
		room.state = room.DAY_BANISH_VOTE
		room.voteList = {}
		room.sendAll({cmd : "banishVote",time : banishVoteTime})
		room.nextTime = (new Date()).getTime() + banishVoteTime
	}
	//第一轮放逐投票结束
	local.banishend1 = function() {
		room.state = room.NONE_STATE
		var maxTarget = -1
		var maxVoteCount = 0
		//记录投票人数
		for(var i in room.voteList){
			if(!room.votedList[room.voteList[i]]){
				room.votedList[room.voteList[i]] = 0
			}
			room.votedList[room.voteList[i]] += 1
			if(i == room.sheriffChair){
				room.votedList[room.voteList[i]] += 0.5
			}
		}
		for(var i in room.votedList){
			if(room.votedList[i] > maxVoteCount){
				maxTarget = i
				maxVoteCount = room.votedList[i]
			}
		}
		if(maxTarget == -1){
			//无人投票、无人出局进入黑夜
			local.banishFinish(false)
		}else{
			var tmpCount = 0
			for(var i in room.votedList){
				if(room.votedList[i] >= maxVoteCount){
					tmpCount++ 
				}
			}
			if(tmpCount > 1){
				//多人平票，第二轮发言
				for(var i in room.votedList){
					if(room.votedList[i] < maxVoteCount){
						delete room.votedList
					}
				}
				room.nextFun = local.banishSpeak2
				room.nextTime = (new Date()).getTime() + 5000
			}else{
				//被放逐玩家死亡、进入遗言阶段
				local.banishFinish(maxTarget)
			}
		}
	}
	//第二轮放逐发言阶段
	local.banishSpeak2 = function() {
		room.state = room.DAY_PLAYER_SAY_1
		room.nextFun = local.banishend2
		var notify = {
			cmd : "banishSpeak2"
		}
		room.sendAll(notify)
	}
	//第二轮放逐投票结束
	local.banishend2 = function() {
		room.state = room.NONE_STATE
		var maxTarget = -1
		var maxVoteCount = 0
		//记录投票人数
		for(var i in room.voteList){
			if(!room.votedList[room.voteList[i]]){
				room.votedList[room.voteList[i]] = 0
			}
			room.votedList[room.voteList[i]] += 1
			if(i == room.sheriffChair){
				room.votedList[room.voteList[i]] += 0.5
			}
		}
		for(var i in room.votedList){
			if(room.votedList[i] > maxVoteCount){
				maxTarget = i
				maxVoteCount = room.votedList[i]
			}
		}
		if(maxTarget == -1){
			//无人投票、无人出局进入黑夜
			local.banishFinish(false)
		}else{
			var tmpCount = 0
			for(var i in room.votedList){
				if(room.votedList[i] >= maxVoteCount){
					tmpCount++ 
				}
			}
			if(tmpCount > 1){
				//多人平票，无人出局，进入黑夜
				for(var i in room.votedList){
					if(room.votedList[i] < maxVoteCount){
						delete room.votedList
					}
				}
				local.banishFinish(false)
			}else{
				//被放逐玩家死亡、进入遗言阶段
				local.banishFinish(maxTarget)
			}
		}
	}
	local.banishFinish = function(chair) {
		console.log("room.nextTime ： ",room.nextTime)
		var notify = {
			cmd : "banishFinish",
			chair : chair
		}
		room.sendAll(notify)
		if(chair !== false){
			room.nextFun = local.nightWerewol
			local.dieDispose(maxTarget,"banish",true)
		}else{
			room.nextFun = local.nightWerewol
			room.nextTime = (new Date()).getTime() + 5000
		}
	}
	//房主开始游戏
	room.handle.startGame = function(uid,sid,param,cb) {
		if(room.state != PRE_STAET){
			cb(false,"不在准备阶段")
			return
		}
		if(uid != room.houseOwner){
			cb(false,"房主才能开始游戏")
			return
		}
		cb(true)
		local.startGame()
	}
	//狼人刀人
	room.handle.werewolfAction = function(uid,sid,param,cb) {
		if(room.state != NIGHT_WEREWOLF_STATE){
			cb(false,"不在狼人阶段")
			return
		}
		var chair = room.chairMap[uid]
		if(roleList[chair] !== "werewolf"){
			cb(false,"身份错误：werewolf : "+roleList[room.chairMap[uid]])
			return
		}
		if(room.surviveState[chair] !== true){
			cb(false,"已死亡")
			return
		}
		var target = param.target
		if(typeof(target) != "number" || target < 0 || target >= roleList.length || room.surviveState[target] !== true){
			cb(false,"目标错误 : "+target)
			return
		}
		room.killList[chair] = target
		var notify = {
			cmd : "werewolfChooseTarget",
			target : target,
			chair : chair
		}
		local.sendByRole("werewolf",notify)
		cb(true)
		//全部狼人已选择则提前结束
		var flag = true
		for(var i = 0;i < roleMap["werewolf"].length;i++){
			if(room.killList[i] != undefined){
				flag = false
				break
			}
		}
		if(flag){
			room.nextTime = (new Date()).getTime()
		}
	}
	//狼人自爆
	room.handle.selfDestruction = function(uid,sid,param,cb) {
		if(room.diurnal !=  "day"){
			cb(false,"不在白天阶段")
			return
		}
		var chair = room.chairMap[uid]
		if(roleList[chair] !== "werewolf"){
			cb(false,"身份错误：werewolf : "+roleList[room.chairMap[uid]])
			return
		}
		if(room.surviveState[chair] !== true){
			cb(false,"已死亡")
			return
		}
		local.dieDispose(chair,"boom",false)
		//警上投票阶段先公布死亡信息
		if(room.state >= DAY_RUNSHERIFF_JOIN && room.state <= DAY_RUNSHERIFF_SAY2){
			room.nextFun = local.nightWerewol
			local.openResult()
		}else{
			room.nextFun = local.nightWerewol
			room.nextTime = 5000
		}
	}
	//预言家验人
	room.handle.seerAction = function(uid,sid,param,cb) {
		if(room.state != NIGHT_SEER_STATE){
			cb(false,"不在预言家阶段")
			return
		}
		var chair = room.chairMap[uid]
		if(roleList[chair] !== "seer"){
			cb(false,"身份错误：seer : "+roleList[room.chairMap[uid]])
			return
		}
		if(room.surviveState[chair] !== true){
			cb(false,"已死亡")
			return
		}
		var target = param.target
		if(typeof(target) != "number" || target < 0 || target >= roleList.length || room.surviveState[target] !== true || target == chair){
			cb(false,"目标错误 : "+target)
			return
		}
		if(roleList[target] == "werewolf"){
			cb(true,"badPerson")
		}else{
			cb(true,"goodPerson")
		}
		room.state = room.NIGHT_WITCH_STATE
		room.nextTime = (new Date()).getTime()
	}
	//女巫操作
	room.handle.witchAction = function(uid,sid,param,cb) {
		if(room.state != NIGHT_WITCH_STATE){
			cb(false,"不在女巫阶段")
			return
		}
		var chair = room.chairMap[uid]
		if(roleList[chair] !== "witch"){
			cb(false,"身份错误：witch : "+roleList[room.chairMap[uid]])
			return
		}
		if(room.surviveState[chair] !== true){
			cb(false,"已死亡")
			return
		}
		if(room.state != NIGHT_WITCH_STATE){
			cb(false,"不在女巫阶段")
			return
		}
		var chair = room.chairMap[uid]
		if(roleList[chair] !== "witch"){
			cb(false,"身份错误：witch : "+roleList[room.chairMap[uid]])
			return
		}
		
		var type = param.type
		if(type == "save"){
			if(!room.witchAgentia[type]){
				cb(false,"已用过")
				return
			}
			room.witchAction["type"] = type
		}else if(type == "poison"){
			if(!room.witchAgentia[type]){
				cb(false,"已用过")
				return
			}
			var target = param.target
			if(typeof(target) != "number" || target < 0 || target >= roleList.length || room.surviveState[target] != true || target == chair){
				cb(false,"目标错误 : "+target)
				return
			}
			room.witchAction["type"] = type
			room.witchAction["target"] = false
		}else{
			cb(false,"type error : "+type)
			return
		}
		cb(true)
		room.nextTime = (new Date()).getTime()
	}
	//获取当前房间信息
	room.handle.getRoomInfo = function(uid,sid,param,cb) {
		var chair = room.chairMap[uid]
		var info = {
		}
		cb(true,info)
	}
	return room
}